package myhashmap;

import java.util.HashMap;

public class MyHashMap<K,V> {


    public MyHashMap(){
    }

    static class Node<K,V> {

        private K k;
        private V v;
        private Node next;

        public Node(K k, V v) {
            this.k = k;
            this.v = v;
        }

        public K getK() {
            return k;
        }

        public void setK(K k) {
            this.k = k;
        }

        public V getV() {
            return v;
        }

        public void setV(V v) {
            this.v = v;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node next) {
            this.next = next;
        }
    }

    Node<K,V>[] tab=new Node[16];


    public void put(K k,V v){

        Node<K,V> tempNode;
        //1.首先计算k的哈希code
        int hashCode = k.hashCode();
        //2.哈希code和当前数组长度n-1进行位运算
        int index = hashCode & (tab.length - 1);
        //3.判断此index上面是不是为空，为空则直接放入，否则遍历链表，并进行equals比对
        if(tab[index] == null){
            tempNode=new Node<>(k,v);
            tab[index]=tempNode;
        }


    }

    public V get(K k){
        //1.首先计算k的哈希code
        int hashCode = k.hashCode();
        //2.哈希code和当前数组长度n-1进行位运算
        int index = hashCode & (tab.length - 1);
        return tab[index].getV();
    }

    public Node<K, V>[] getTab() {
        return tab;
    }

    public void setTab(Node<K, V>[] tab) {
        this.tab = tab;
    }
}
